# Importing
from __future__ import division
from time import time
print 'Plotting data. Developing alpha version.'
print
print 'Importing'

t0=time()
import matplotlib
matplotlib.use('TkAgg')           # Backend.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from numpy import array, dot, linalg, asarray
import psyco
import os
import ConfigParser

import silenus
import hrun


print
print 'Using', silenus.__version__, 'and', hrun.__version__
print

t1=time()
psyco.full()
t2=time()

config = ConfigParser.ConfigParser()
config.read('config.ini')

# Parameters
dbfilename=config.get('Setting up', 'dbfilename')
mode=int(config.get('Plotting', 'mode'))

print "Importing and formatting"
# Load data
data=silenus.import_data(dbfilename)

# Reformatting data
ncenters=len(data[0])
centers=[]
for i in xrange(ncenters):
    centers.append([])

for frame in data:
    for i in xrange(ncenters):
        centers[i].append(frame[i])

#centers=hrun.relax(centers, 5)
t3=time()

# Generate plotting
print "Plotting."
if mode==1:             # Mode 1 is for pendulum.
    fc=1
    colors=['r', 'b']
    
    fig=plt.figure(fc)
    ax=fig.add_subplot(111)
    for i in xrange(len(centers)):
        point=centers[i]
        x_coord=[x[0] for x in point]
        hrun.normalize(x_coord)
        plt.plot(range(len(point)), x_coord, colors[i])
    ax.set_title('Both pendulums, normalized')
    plt.xlabel('Time (AU)')
    plt.ylabel('px')
    plt.savefig('X-coord_both_normalized.png')
    
    
if mode==2:             # Mode 2 is for spring.
    colors=['r', 'b', 'g', 'c', 'y']
    for i in xrange(len(centers)):
        fc=i
        fig=plt.figure(fc)
        ax=fig.add_subplot(111)
        point=centers[i]
          
        plt.plot(range(len(point)), [x[0] for x in point],
                  alpha=0.6, color=colors[i])
        
        ax.set_title('Spring point '+str(i+1
                        )+", dumped oscillation.")
        plt.xlabel('time')
        plt.ylabel('px')
        plt.savefig('X-coord_'+str(i+1)+'.png')
    
    
    fc+=1
    fig=plt.figure(fc)
    ax=fig.add_subplot(111)
    
    for i in xrange(len(centers)):
        point=centers[i]
        x_coord=[x[0] for x in point]
        x_coord=hrun.normalize(x_coord)
        plt.plot(range(len(point)), x_coord, colors[i],
                  alpha=0.3)
    ax.set_title('Spring, all control points.')
    plt.xlabel('time')
    plt.ylabel('px')
    plt.savefig('X-coord_spring_all.png')
    
    
    fc+=1
    fig=plt.figure(fc)
    ax=fig.add_subplot(111)
    
    point=centers[0]
    x_total=[x[0] for x in point]
    x_total=hrun.normalize(x_total)
    for i in xrange(1, len(centers)):
        point=centers[i]
        x_coord=[x[0] for x in point]
    
        x_coord=hrun.normalize(x_coord)
        x_total=hrun.add(x_coord, x_total)
    
    plt.plot(range(len(point)), x_total, alpha=0.6)
    ax.set_title('All spring points, normalized')
    plt.xlabel('time')
    plt.ylabel('px')
    plt.savefig('Average.png')

t4=time()
print 'Finished'
print
print 'Time spent:'
print str(t1-t0), 's importing modules.'
print str(t2-t1), 's compiling.'
print str(t3-t2), 's loading and reformatting data.'
print str(t4-t3), 's iterating.'
print
plt.show()